package com.stardata.liangshisq.tcslmon.job;

import com.stardata.liangshisq.tcslmon.service.ItemQueueService;
import com.stardata.liangshisq.tcslmon.service.ObjectSyncLogService;
import com.stardata.liangshisq.tcslmon.service.SyncStatusService;
import com.stardata.liangshisq.tcslmon.util.CommonConstants;
import com.stardata.liangshisq.tcslmon.util.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Component
public class OrderSynchronizeTask {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private SyncStatusService syncStatusService;
    @Autowired
    private ObjectSyncLogService objectSyncLogService;

    @Autowired
    private ItemQueueService itemQueueService;

    private static final String SYNC_ORDER_TOKEN = "syncOrderToken";
    private static final String NOTIFY_KITCHEN_TOKEN = "notifyKitchenToken";
    private static final String REFRESH_ITEM_QUEUE_TOKEN = "refreshItemQueue";

    @Scheduled(cron = "0/5 * * * * ?")
    public void doRefreshItemQueue() {
        try {
            logger.info("=====>>>>>使用scheduled  {}", System.currentTimeMillis());
            refreshItemQueue();
        } catch (Exception ex) {

        }
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void orderRefresh() {
        try {
            logger.info("=====>>>>>使用scheduled  {}", System.currentTimeMillis());
            // itemQueueService.orderItemRefresh();

        } catch (Exception ex) {

        }
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void messageSend() {
        try {
            logger.info("=====>>>>>使用scheduled  {}", System.currentTimeMillis());
            itemQueueService.messageSend();

        } catch (Exception ex) {

        }
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void doNotifyKitchen() {
        try {
            logger.info("=====>>>>>使用scheduled2  {}", System.currentTimeMillis());
            notifyKitchen();
        } catch (Exception ex) {

        }
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void doSynchronizeBizBsToCloudServer() {
        try {
            logger.info("=====>>>>>使用scheduled3  {}", System.currentTimeMillis());
            synchronizeBizBsToCloudServer();
        } catch (Exception ex) {

        }
    }


    private void synchronizeBizBsToCloudServer() {
        logger.info(" INFO===> 开始同步营业流水数据到云服务器 ...........");

        //1. 获得最近更新的同步日志id
        Long lastSyncLogId = syncStatusService
                .getLastSyncId(CommonConstants.ObjectType.ORDER);
        logger.debug(String.format("<=== 当前订单流水的最新同步日志ID: %s ", lastSyncLogId));

        //2. 获得所有待同步更新的、不重复的BizBsid列表
        List<Long> bizBsIdsForSync = new ArrayList<>();
        Long newLastSyncLogId = objectSyncLogService.queryBizBsIdsForSync(
                lastSyncLogId, bizBsIdsForSync);

        logger.debug("<=== 需要同步的营业流水bsId列表: " + JSONUtil.toJSONString(bizBsIdsForSync));

        //3. 在一个事务中，执行同步操作，并记录最新订单id到 SyncStatus 表
        objectSyncLogService.synchronizeBizBs(newLastSyncLogId, bizBsIdsForSync);

        logger.info("<=== 完成营业流水数据同步...........");
    }

    private void notifyKitchen() {
        logger.info(" INFO ===> 开始通知v1版后厨界面刷新...........");

        //1. 获得最近更新的菜品项同步id
        Long lastSyncLogId = syncStatusService
                .getLastSyncId(CommonConstants.ObjectType.ORDER_ITEM);
        logger.info(String.format("<=== 当前菜品项流水的最新同步日志ID: %s ", lastSyncLogId));

        //2. 获得所有待通知的、不重复的订单菜品项id列表
        List<Map<String, List<Long>>> notifications = new ArrayList<>();
        Long newLastSyncLogId = objectSyncLogService.queryItemNotifications(
                lastSyncLogId, notifications);

        logger.info("<=== v1版后厨界面刷新通知列表: " + JSONUtil.toJSONString(notifications));

        //3. 在一个事务中，发送后厨终端通知、并记录最新订单id到 SyncStatus 表
        objectSyncLogService.notifyKitchen(newLastSyncLogId, notifications);

    }

    private void refreshItemQueue() {
        logger.info(" INFO ===> 开始v2版后厨配菜重新排队...........");

        //1. 获得最近更新的菜品项同步id
        Long lastSyncLogId = syncStatusService
                .getLastSyncId(CommonConstants.ObjectType.ORDER_ITEM_QUEUE);
        logger.info(String.format("<===  当前菜品项排队流水的最新同步日志ID: %s ", lastSyncLogId));

        //2. 获得所有待通知的、不重复的订单菜品项id列表
        List<Map<String, List<Long>>> notifications = new ArrayList<>();
        Long newLastSyncLogId = objectSyncLogService.queryItemNotifications(
                lastSyncLogId, notifications);

        //3. 在一个事务中，发送后厨终端通知、并记录最新订单id到 SyncStatus 表
        objectSyncLogService.refreshItemOueue(newLastSyncLogId, notifications);

        logger.info("<=== 完成v2版后厨配菜重新排队...........");
    }

}
